Skip to contentMethod: createDataPropertyLiteral(Object, String, ValueFactory)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.sesame;
16:
17: import cz.cvut.kbss.ontodriver.model.NamedResource;
18: import org.openrdf.model.*;
19: import org.openrdf.model.URI;
20: import org.openrdf.model.vocabulary.RDF;
21: import org.openrdf.model.vocabulary.XMLSchema;
22: import org.slf4j.LoggerFactory;
23:
24: import java.net.*;
25: import java.util.Date;
26:
27: /**
28: * Utility methods for the Sesame driver.
29: *
30: * @author ledvima1
31: */
32: public final class SesameUtils {
33:
34: private SesameUtils() {
35: // Private constructor
36: }
37:
38: /**
39: * Gets value of the specified data property literal as the corresponding Java object. Primitives are returned
40: * boxed.
41: *
42: * @param literal DataProperty value
43: * @return Java value corresponding to the XML Schema datatype of the literal
44: * @throws IllegalArgumentException If literal's datatype is not supported
45: */
46: public static Object getDataPropertyValue(Literal literal) {
47: assert literal != null;
48:
49: final URI datatype = literal.getDatatype();
50: if (datatype == null || datatype.equals(XMLSchema.STRING)
51: || datatype.equals(XMLSchema.NORMALIZEDSTRING) || datatype.equals(RDF.LANGSTRING)) {
52: return literal.stringValue();
53: } else if (datatype.equals(XMLSchema.INT) || datatype.equals(XMLSchema.UNSIGNED_INT)) {
54: return literal.intValue();
55: } else if (datatype.equals(XMLSchema.INTEGER)
56: || datatype.equals(XMLSchema.POSITIVE_INTEGER)
57: || datatype.equals(XMLSchema.NON_NEGATIVE_INTEGER)
58: || datatype.equals(XMLSchema.NEGATIVE_INTEGER)
59: || datatype.equals(XMLSchema.NON_POSITIVE_INTEGER)) {
60: return literal.intValue();
61: } else if (datatype.equals(XMLSchema.BOOLEAN)) {
62: return literal.booleanValue();
63: } else if (datatype.equals(XMLSchema.LONG) || datatype.equals(XMLSchema.UNSIGNED_LONG)) {
64: return literal.longValue();
65: } else if (datatype.equals(XMLSchema.DECIMAL)) {
66: return literal.decimalValue();
67: } else if (datatype.equals(XMLSchema.DOUBLE)) {
68: return literal.doubleValue();
69: } else if (datatype.equals(XMLSchema.SHORT) || datatype.equals(XMLSchema.UNSIGNED_SHORT)) {
70: return literal.shortValue();
71: } else if (datatype.equals(XMLSchema.BYTE) || datatype.equals(XMLSchema.UNSIGNED_BYTE)) {
72: return literal.byteValue();
73: } else if (datatype.equals(XMLSchema.DATE) || datatype.equals(XMLSchema.DATETIME)) {
74: return literal.calendarValue().toGregorianCalendar().getTime();
75: } else {
76: throw new IllegalArgumentException("Unsupported datatype " + datatype);
77: }
78: }
79:
80: /**
81: * Creates Sesame literal from the specified value, which can be used as data property object.
82: *
83: * @param value The value to transform
84: * @param language Language to add to string literals
85: * @param vf Sesame value factory
86: * @return Sesame Literal
87: * @throws IllegalArgumentException If the type of the value is not supported
88: */
89: public static Literal createDataPropertyLiteral(Object value, String language, ValueFactory vf) {
90:• assert value != null;
91:
92:• if (value instanceof Integer) {
93: return vf.createLiteral((Integer) value);
94:• } else if (value instanceof String) {
95: return vf.createLiteral((String) value, language);
96:• } else if (value instanceof Byte) {
97: return vf.createLiteral((Byte) value);
98:• } else if (value instanceof Short) {
99: return vf.createLiteral((Short) value);
100:• } else if (value instanceof Boolean) {
101: return vf.createLiteral((Boolean) value);
102:• } else if (value instanceof Double) {
103: return vf.createLiteral((Double) value);
104:• } else if (value instanceof Long) {
105: return vf.createLiteral((Long) value);
106:• } else if (value instanceof Date) {
107: return vf.createLiteral((Date) value);
108:• } else if (value.getClass().isEnum()) {
109: return vf.createLiteral(value.toString());
110: } else {
111: throw new IllegalArgumentException("Unsupported literal type " + value.getClass());
112: }
113: }
114:
115: /**
116: * Checks whether the specified value is a blank node.
117: *
118: * @param value The value to check
119: * @return {@code true} if the value is a blank node, {@code false} otherwise
120: */
121: public static boolean isBlankNode(Value value) {
122: assert value != null;
123: return value instanceof BNode;
124: }
125:
126: /**
127: * Resolves whether the specified value is a resource identifier.
128: * <p>
129: * Only absolute IRIs are supported (i.e. no blank node identifiers).
130: *
131: * @param value The value to check
132: * @return {@code true} if the value is either an URI or an URL
133: */
134: public static boolean isResourceIdentifier(Object value) {
135: if (value instanceof NamedResource || value instanceof java.net.URI || value instanceof URL) {
136: return true;
137: }
138: if (!(value instanceof String)) {
139: return false;
140: }
141: try {
142: final java.net.URI uri = java.net.URI.create(value.toString());
143: return uri.isAbsolute();
144: } catch (IllegalArgumentException e) {
145: return false;
146: }
147: }
148:
149: /**
150: * Constructs a Sesame URI from the specified java.net.URI.
151: *
152: * @param javaUri The uri to convert
153: * @param factory ValueFactory used for the conversion
154: * @return Sesame URI
155: */
156: public static URI toSesameUri(java.net.URI javaUri, ValueFactory factory) {
157: return (javaUri != null ? factory.createURI(javaUri.toString()) : null);
158: }
159:
160: public static java.net.URI toJavaUri(Resource resource) {
161: if (resource instanceof BNode) {
162: // We have to check for BNode explicitly, because java's URI treats
163: // BNode's identifier as a valid URI
164: return null;
165: }
166: try {
167: return java.net.URI.create(resource.stringValue());
168: } catch (IllegalArgumentException e) {
169: // This shouldn't happen
170: LoggerFactory.getLogger(SesameUtils.class).error("Sesame resource is not a valid URI: " + e);
171: return null;
172: }
173: }
174: }